<?php

namespace app\api\controller;

use app\common\controller\Api;
use think\Db;

/**
 * 首页接口
 */
class Report extends Api
{
    protected $noNeedLogin = [''];
    protected $noNeedRight = ['*'];

    /**
     * 首页
     *
     */
    public function index()
    {
        $this->success('请求成功');
    }
    public function add(){
        $user = $this->auth->getUser();
        $content = $this->request->post('content');
        $name = $this->request->post('name');
        if(!$content){
            $this->error("缺少比传参数");
        }
        $in_data['uid'] = $user->id;
        $in_data['content'] = $content;
        $in_data['name'] = $name;
        $in_data['createtime'] = time();
        $in_data['updatetime'] = time();

        $in_status = Db::name("report")->insertGetId($in_data);
        if($in_status){
            $data = Db::name("report")->where(['id'=>$in_status])->find();
            $this->success("成功",$data);
        }else{
            $this->error("失败");
        }


    }
    public function details(){
        $user = $this->auth->getUser();
        $data = Db::name("report")->where(['uid'=>$user->id])->select();

        foreach($data as $k=>$v){
            $arr = explode(',',$v['content']);
            $tsr = '';
            foreach ($arr as $k1=>$v1){
                $tsr .= cdnurl($v1, true).",";
            }
            $data[$k]['content'] = trim($tsr,',');
        }

        $this->success("成功",$data);
    }
    public function edit(){
        $user = $this->auth->getUser();
        $content = $this->request->post('content');
        $name = $this->request->post('name');
        $id = $this->request->post('id');
        if(!$content || !$id){
            $this->error("缺少比传参数");
        }
        $data = Db::name("report")->where(['uid'=>$user->id,'id'=>$id])->find();
      if(!$data){
          $this->error("数据有误");
      }
            $in_data['content'] = $content;
            $in_data['name'] = $name;
            $in_data['updatetime'] = time();
            $in_status = Db::name("report")->where(['id'=>$data['id']])->update($in_data);
            if($in_status !== false){
                $this->success("成功");
            }else{
                $this->error("失败");
            }
    }
    public function delete(){
        $user = $this->auth->getUser();
        $id = $this->request->post('id');
        if(!$id){
            $this->error("缺少比传参数");
        }
        $data = Db::name("report")->where(['uid'=>$user->id,'id'=>$id])->find();
        if(!$data){
            $this->error("数据有误");
        }
        Db::name("report")->where(['id'=>$data['id']])->delete();
        $this->success("删除成功");
    }
}
